from django.shortcuts import render
from Merchant.forms import RegisterForm,LoginForm,ChangepwdForm
from django.contrib.auth import authenticate
from django.contrib import auth
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
# Create your views here.

@login_required
def merchant_index(request):
    username = request.session.get('username','anybody')
    return render(request, 'merchant/merchant_index.html',{'user':username})

def merchant_login(request):
    error= []
    if request.method=='POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            username = data['username']
            password = data['password']
            user = auth.authenticate(username=username, password=password)
            if user is not None and user.is_active:
                auth.login(request, user)
                request.session['username']= username
                return HttpResponseRedirect(reverse('merchant:merchant_index'))
            else:
                error.append('Please input the correct password')
        else:
            error.append('Please input both username and password')
    else:
        form = LoginForm()
    context = {
        'error':error,
        'form':form
    }
    return render(request, 'merchant/merchant_login.html', context)

def merchant_registe(request):
    error = []
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            username = data['username']
            email = data['email']
            password = data['password']
            password2 = data['password2']
            if not User.objects.all().filter(username=username):
                if form.pwd_validate(password, password2):
                    user = User.objects.create_user(username, email, password)
                    user.save()
                    user = auth.authenticate(username=username, password=password)
                    if user is not None and user.is_active:
                        auth.login(request,user)
                        request.session['username']= username
                        return HttpResponseRedirect(reverse('merchant:merchant_index'))
                else:
                    error.append('Please input the same password')
            else:
                error.append('The username has existed,please change your username')
        else:
            error.append('Please input all the information')
    else:
        form = RegisterForm()
    context = {
        'form':form,
        'error':error
    }
    return render(request, 'merchant/merchant_register.html',context)

def merchant_logout(request):
    auth.logout(request)
    return HttpResponseRedirect(reverse('merchant:merchant_login'))

def merchant_changepassword(request, username):
    error = []
    if request.method == 'POST':
        form = ChangepwdForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            user = authenticate(username=username,password=data['old_pwd'])
            if user is not None:
                if data['new_pwd']==data['new_pwd2']:
                    newuser = User.objects.get(username__exact=username)
                    newuser.set_password(data['new_pwd'])
                    newuser.save()
                    return HttpResponseRedirect(reverse('merchant:merchant_login'))
                else:
                    error.append('Please input the same password')
            else:
                error.append('Please correct the old password')
        else:
            error.append('Please input the required domain')
    else:
        form = ChangepwdForm()
    return render(request, 'merchant/merchant_changepassword.html',{'form':form,'error':error})